package org.example.controller.flow;

import org.example.controller.flow.service.FlowService;
import org.example.vo.ReturnVo;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
public class FlowController {

    @Autowired
    FlowService flowService;

    @GetMapping(value = "/startFlow/{username}",produces = "application/json")
    public ReturnVo<String> StartFlow(@PathVariable(value = "username") String username, @RequestParam String processDefinitionKey) {
        ReturnVo returnVo = flowService.startFlow(processDefinitionKey, username);
        return returnVo;
    }

    @GetMapping(value = "/getNowTask/{processId}",produces = "application/json")
    public ReturnVo<String> getNowTask(@PathVariable(value = "processId") String processId) {
        ReturnVo returnVo = flowService.getNowTask(processId);
        return returnVo;
    }

    @GetMapping(value = "/complete",produces = "application/json")
    public ReturnVo<String> complete(@RequestBody CompleteTaskVo completeTaskVo) {
        ReturnVo returnVo = flowService.complete(completeTaskVo);
        return returnVo;
    }

    @GetMapping(value = "/stop/{processId}",produces = "application/json")
    public ReturnVo<String> stop(@PathVariable(value = "processId") String processId) {
        Boolean aBoolean = flowService.stopProcess(processId);
        System.out.printf("b"+aBoolean);
        return new ReturnVo<>();
    }

    @GetMapping(value = "/turnProcess",produces = "application/json")
    public ReturnVo<String> turnProcess(@RequestParam String processInstanceId,@RequestParam String assignee) {
        Boolean aBoolean = flowService.turnProcess(processInstanceId, assignee);
        System.out.printf("b"+aBoolean);
        return new ReturnVo<>();
    }

    @GetMapping(value = "/delegation",produces = "application/json")
    public ReturnVo<String> delegation(@RequestParam String processInstanceId,@RequestParam String assignee) {
        Boolean aBoolean = flowService.delegation(processInstanceId, assignee);
        System.out.printf("b"+aBoolean);
        return new ReturnVo<>();
    }

    @GetMapping(value = "/getHisNode",produces = "application/json")
    public ReturnVo<String> getHisNode(@RequestParam String processInstanceId) {
        List<FlowNodeVo> backNodesByProcessInstanceId = flowService.getBackNodesByProcessInstanceId(processInstanceId);
        System.out.printf("b"+backNodesByProcessInstanceId.toString());
        return new ReturnVo<>("200","ok",backNodesByProcessInstanceId.toString());
    }

    @GetMapping(value = "/backToStepTask",produces = "application/json")
    public ReturnVo<String> backToStepTask(@RequestParam String taskId,@RequestParam String username,@RequestParam String toStep) {
        ReturnVo<String> stringReturnVo = flowService.backToStepTask(taskId, username, toStep);
        return new ReturnVo<>("200","ok");
    }
}
